#include "os_init.h"
#include "../data_struct/head.h"
#include "../global/head.h"

//////////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace sgsj_client
{

    os_init::os_init()
    {
        m_pAioMgr = 0;
        m_pCommon = 0;
        m_pXos = 0;
        m_pCurlMgr = 0;
        m_pEncryptMgr = 0;
        m_pHttpMgr = 0;
        m_pJsonMgr = 0;
        m_pLog = 0;
        m_pSqliteMgr = 0;
        m_pXmlMgr = 0;
        m_pPackageMgr = 0;
        m_pPackage = 0;

        m_pCallbackThread = 0;
        m_pMisc = 0;
        m_pCrt = 0;
    }

    os_init::~os_init()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int os_init::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            ret = m_GetCore.init();
            if( 0 == ret )
            {
                m_pXos = m_GetCore.m_pApi;
            }
        }

        if( 0 == ret )
		{
			ret = m_pXos->create( xos::i_xos::XOS_OBJ_CALLBACK, ( void ** )&m_pCallbackThread );
		}

        if( 0 == ret )
        {
            ret = m_pXos->create( xos::i_xos::XOS_OBJ_MISC, ( void ** )&m_pMisc );
		}

        if( 0 == ret )
        {
            ret = m_pXos->create( xos::i_xos::XOS_OBJ_CRT, ( void ** )&m_pCrt );
        }

		if( 0 == ret )
		{
			m_pMisc->get_process_path( g_pSysData->m_szExePath, sizeof( g_pSysData->m_szExePath ), 0 );
		}

		if( 0 == ret )
        {
            ret = m_GetCommon.init();
        }

        if( 0 == ret )
        {
            ret = m_GetXml.init();
        }

        if( 0 == ret )
        {
            ret = m_GetLog.init();
        }

        if( 0 == ret )
        {
            ret = m_GetAio.init();
        }

        if( 0 == ret )
        {
            ret = m_GetCurl.init();
        }

        if( 0 == ret )
        {
            ret = m_GetEncrypt.init();
        }

        if( 0 == ret )
        {
            ret = m_GetPackage.init();
        }

		if( 0 == ret )
        {
            ret = m_GetHttp.init();
        }

		if( 0 == ret )
        {
            ret = m_GetJson.init();
        }

		if( 0 == ret )
        {
            ret = m_GetSqlite.init();
        }

        return ret;
    }

    int os_init::term()
    {
        int ret = 0;

        m_GetSqlite.term();
        m_GetJson.term();
        m_GetHttp.term();
        m_GetPackage.term();
        m_GetEncrypt.term();
        m_GetCurl.term();
		m_GetAio.term();
        m_GetLog.term();
        m_GetXml.term();
		m_GetCommon.term();

        if( m_pCallbackThread )
		{
			m_pCallbackThread->release();
			m_pCallbackThread = 0;
		}

        if( m_pMisc )
        {
            m_pMisc->release();
            m_pMisc = 0;
        }

        if( m_pCrt )
        {
            m_pCrt->release();
            m_pCrt = 0;
        }

        m_GetCore.term();

        return ret;
    }

} // sgsj_client
